package com.badshun.player;

import com.badshun.util.*;
import com.badlogic.gdx.*;
import java.util.*;
import com.badlogic.gdx.math.*;
import com.badlogic.gdx.graphics.*;
import com.badshun.box2d.*;

public class NPCPlayers {
	private BadDataLibrary bdl;
	private HashMap<Integer,HashMap<String,String>> totalMap;
	
	private Box2DWorld world;
	private OrthographicCamera camera;
	
	public BadList<NPCPlayer> list;
	
	public NPCPlayers(String TAG,MainPlayer mainPlayer,Box2DWorld world,String dataPath,OrthographicCamera camera) {
		this.world = world;
		this.camera = camera;
		
		defineNPCPlayers(TAG,mainPlayer,dataPath);
	};
	
	private void defineNPCPlayers(String TAG,MainPlayer mainPlayer,String dataPath) {
		bdl = new BadDataLibrary(Gdx.files.internal(dataPath));
		
		totalMap = bdl.getData();
		
		createNPC(mainPlayer,TAG);
	}
	
	private void createNPC(MainPlayer mainPlayer,String TAG) {
		list = new BadList<NPCPlayer>();
		
		for(int i = 0; i < totalMap.size(); i++) {
			HashMap<String,String> dataMap = totalMap.get(i);
			
			String name = dataMap.get("Name");
			String head = dataMap.get("Head");
			String action = dataMap.get("Action");
			String direction = dataMap.get("Direction");
			String player = dataMap.get("Player");
			String position = dataMap.get("Position");
			String talk = dataMap.get("Talk");
			
			NPCPlayer.ACTION actionForNPC = null;
			NPCPlayer.DIRECTION directionForNPC = null;
			
			Texture headImage = new Texture(Gdx.files.internal(head));
			
			if(action.equals("MOVE")) {
				actionForNPC = NPCPlayer.ACTION.MOVE;
			} else if(action.equals("TALK")) {
				actionForNPC = NPCPlayer.ACTION.TALK;
			} else if(action.equals("IDLE")) {
				actionForNPC = NPCPlayer.ACTION.IDLE;
			}
			
			if(direction.equals("LEFT")) {
				directionForNPC = NPCPlayer.DIRECTION.LEFT;
			} else if(direction.equals("RIGHT")) {
				directionForNPC = NPCPlayer.DIRECTION.RIGHT;
			} else if(direction.equals("UP")) {
				directionForNPC = NPCPlayer.DIRECTION.UP;
			} else if(direction.equals("DOWN")) {
				directionForNPC = NPCPlayer.DIRECTION.DOWN;
			}
			
			Vector2 positionForNPC = new Vector2(new Float(position.split("\\|")[0]),new Float(position.split("\\|")[1]));
			
			String[] talkContent = talk.split("\\|");
			
			NPCPlayer npc = new NPCPlayer(TAG,mainPlayer,headImage,name,actionForNPC,directionForNPC,world,player,positionForNPC,camera,talkContent);
			
			list.addChild(npc);
		}
	}
	
	public void render() {
		for(int i = 0; i < list.getLength(); i++) {
			list.getChild(i).render();
		}
	}
}
